package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.web.util.CookiesUtil;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserInfoController {
    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("/hello")
    @ResponseBody
    @RequireLogin
    public UserInfo get(Long id){
        return userInfoService.get(id);
    }

    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){
        return !userInfoService.checkPhone(phone);
    }

    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone,String nickname,String password,String rpassword,String verifyCode){
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(HttpServletRequest request, HttpServletResponse response,String username, String password){
        String token = userInfoService.userLogin(username,password);
        //把token缓存到cookie中
        CookiesUtil.addCookie("token",token,response,RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());
        /*Cookie cookie = new Cookie("token",token);
        cookie.setPath("/");
        cookie.setMaxAge(RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());
        response.addCookie(cookie);*/
        return AjaxResult.SUCCESS;
    }
}
